"""
数据验证工具函数
"""

import re
from typing import Any, Union

def validate_input(data, data_type=None, min_length=None, max_length=None):
    """
    验证输入数据
    
    Args:
        data: 要验证的数据
        data_type: 期望的数据类型
        min_length: 最小长度
        max_length: 最大长度
    
    Returns:
        bool: 验证结果
    """
    if data is None:
        return False
    
    # 类型检查
    if data_type and not isinstance(data, data_type):
        return False
    
    # 长度检查（适用于字符串、列表等）
    if hasattr(data, '__len__'):
        length = len(data)
        if min_length is not None and length < min_length:
            return False
        if max_length is not None and length > max_length:
            return False
    
    return True

def validate_email(email):
    """
    验证邮箱地址格式
    
    Args:
        email: 邮箱地址字符串
    
    Returns:
        bool: 验证结果
    """
    if not isinstance(email, str):
        return False
    
    pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    return re.match(pattern, email) is not None

def validate_phone(phone):
    """
    验证中国手机号码格式
    
    Args:
        phone: 手机号码字符串
    
    Returns:
        bool: 验证结果
    """
    if not isinstance(phone, str):
        return False
    
    pattern = r'^1[3-9]\d{9}$'
    return re.match(pattern, phone) is not None

def validate_range(value, min_val=None, max_val=None):
    """
    验证数值范围
    
    Args:
        value: 要验证的数值
        min_val: 最小值
        max_val: 最大值
    
    Returns:
        bool: 验证结果
    """
    try:
        value = float(value)
        if min_val is not None and value < min_val:
            return False
        if max_val is not None and value > max_val:
            return False
        return True
    except (ValueError, TypeError):
        return False 